
const env = require('./env.js')

const request_get = (url, params, callback) => {
  wx.request({
    url: env.baseURL + url,
    data: params || {},
    method: 'GET',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      const {code, data, msg} = res.data;
      if (code === 200) {
        callback(true, data, null)
      } else {
        callback(false, null, msg)
      }
    },
    fail: (err) => {
      callback(false, null, err || '网络失败')
    }
  })
}

const request_post = (url, params, callback) => {
  wx.request({
    url: env.baseURL + url,
    data: params,
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    }, // 设置请求的 header
    success: function (res) {
      const {code, data, msg} = res.data;
      if (code === 200) {
        callback(true, data, null)
      } else {
        callback(false, null, msg)
      }
    },
    fail: (err) => {
      callback(false, null, err || '网络失败')
    }
  })
}

module.exports = {
  request_get,
  request_post
}
